﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Web;
using System.Collections.Specialized;
using System.Collections;
using System.IO;

namespace Marbles.Core.Web.SiteMapManager
{
    public class SiteMapWriter : SiteMapBase
    {

        public XmlDocument Document
        {
            get { return xdoc; }
        }


        private string _filename = string.Empty;
        private XmlDocument xdoc = null;

        public SiteMapWriter(string Filename)
        {
            this._filename = Filename;
        }

        public void Save(SiteMapNode Nodes)
        {
            string mappedfilename = HttpContext.Current.Server.MapPath(this._filename);
            xdoc = new XmlDocument();
            XmlDeclaration docroot = xdoc.CreateXmlDeclaration("1.0", "utf-8", "yes");
            XmlNode root = CreateSiteMapRootNode();
            this._nodes = Nodes;
            root.AppendChild(CreateSiteMapNode(Nodes));

            if (File.Exists(mappedfilename))
            {
                string bakfile = mappedfilename + ".bak";
                if (File.Exists(bakfile)) { File.Delete(bakfile); }
                File.Copy(mappedfilename, bakfile);
                File.Delete(mappedfilename);
            }

            xdoc.AppendChild(docroot);
            xdoc.AppendChild(root);
            xdoc.Save(mappedfilename);
        }

        public XmlNode CreateSiteMapRootNode()
        {
            XmlNode node = xdoc.CreateElement("siteMap", "http://schemas.microsoft.com/AspNet/SiteMap-File-1.0");
            return node;
        }

        public XmlNode CreateSiteMapNode(string url, string title, string description, string resourceKey, string roles)
        {
            XmlElement node = xdoc.CreateElement("siteMapNode");
            node.Attributes.Append(CreateAttribute("url", url));
            node.Attributes.Append(CreateAttribute("title", title));
            node.Attributes.Append(CreateAttribute("description", description));
            node.Attributes.Append(CreateAttribute("resourceKey", resourceKey));
            node.Attributes.Append(CreateAttribute("roles", roles));
            return node;
        }

        public XmlNode CreateSiteMapNode(SiteMapNode node)
        {
            XmlElement node1 = xdoc.CreateElement("siteMapNode");
            node1.Attributes.Append(CreateAttribute("url", node.Url));
            node1.Attributes.Append(CreateAttribute("title", node.Title));
            node1.Attributes.Append(CreateAttribute("description", node.Description));
            node1.Attributes.Append(CreateAttribute("resourceKey", node.ResourceKey));
            node1.Attributes.Append(CreateAttribute("roles", GetStringValue(node.Roles)));

            if (node.ChildNodes.Count > 0)
            {
                foreach (SiteMapNode childnode in node.ChildNodes)
                {
                    node1.AppendChild(CreateSiteMapNode(childnode));
                }
            }

            return node1;
        }

        private string GetStringValue(IList list)
        {
            if (list == null) { return string.Empty; }
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < list.Count; i++)
            {
                if (i > 0) { sb.Append(","); }
                sb.Append(list[i].ToString());
            }
            return sb.ToString();
        }

        private XmlAttribute CreateAttribute(string name, string value)
        {
            XmlAttribute attr = xdoc.CreateAttribute(name);
            attr.InnerText = value;
            return attr;
        }
    }
}
